//
//  LocationManager.m
//  OrderSeat
//
//  Created by 王威 on 13-11-25.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "LocationManager.h"

@implementation LocationManager

- (id)initWithDelegate:(id)delegate
{
    self = [super init];
    if (self) {
        _delegate = delegate;
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.distanceFilter = 1000;
    }
    return self;
}

- (void)startUpdateLocation
{
    [_locationManager startUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    if (_delegate) {
        [_delegate locationManager:manager didUpdateToLocation:newLocation fromLocation:oldLocation];
    }
    /*//    当前的经度
    NSString *currentLatitude = [[NSString alloc]
                                 initWithFormat:@"%g",
                                 newLocation.coordinate.latitude];
    
    //    当前的纬度
    NSString *currentLongitude = [[NSString alloc]
                                  initWithFormat:@"%g",
                                  newLocation.coordinate.longitude];
    
    //    当前的水平距离
    NSString *currentHorizontalAccuracy =
    [[NSString alloc]
     initWithFormat:@"%g",
     newLocation.horizontalAccuracy];
    
    NSString *currentAltitude = [[NSString alloc]
                                 initWithFormat:@"%g",
                                 newLocation.altitude];
    
    NSString *currentVerticalAccuracy =
    [[NSString alloc]
     initWithFormat:@"%g",
     newLocation.verticalAccuracy];
    
    if (startLocation == nil)
        self.startLocation = newLocation;
    
    CLLocationDistance distanceBetween = [newLocation
                                          distanceFromLocation:startLocation];
    
    NSString *tripString = [[NSString alloc]
                            initWithFormat:@"%f",
                            distanceBetween];
    distance.text = tripString;
    
    //定位城市通过CLGeocoder
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        for (CLPlacemark * placemark in placemarks) {
            
            NSString *test = [placemark locality];
            NSLog(@"%@", test);
        }
    }];*/
}

@end
